package com.java.Service;

import com.alibaba.fastjson.JSON;
import com.java.bean.TestInfoBean;
import com.java.bean.storeInfo;
import com.java.config.RedisConfig;
import com.java.util.PatternUtil;
import com.java.util.SerializeUtil;
import com.java.util.TestRedisPool;
import com.scala.ExcelDataInput.DataInputFromExcel;
import org.apache.calcite.runtime.Resources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @Author: ZhangJin
 * @Date: 2020/9/20 22:52
 */
/* 异步线程处理文件 */
@Service
@EnableAsync
public class ReceiveFileFromBefore {

    @Autowired
    DataInputFromExcel dataInputFromExcel;

    public static final String SQLFORDATA = "(UUid String,Local String,Title String,NumOfSeePepple String,PriceByMonth String,AreaOfHouse String,Width String,Depth String,Sheer String,Type String,StateNow String,LocationDetail String,Introduction String,WebSite String,LocationOfLatAndLong String)";

    public static final String NearByStore = "(UUid String,nameOfStore String,Url String,Location String,consumerByAvg String,rateStore String,numberCommit String,Type String,LocationOfLatAndLong String)";

    @Async(value = "taskExecutor2")
    public void AsyncCopeFile(MultipartFile multipartFile,String fileName){
        InputStream inputStream = null;
        try {
            inputStream = multipartFile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        dataInputFromExcel.getDataFromExcel(inputStream,fileName,SQLFORDATA);
    }

    @Async(value = "taskExecutor2")
    public void AsyncCopeFileNearByInfo(MultipartFile multipartFile,String fileName){
        InputStream inputStream = null;
        try {
            inputStream = multipartFile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        dataInputFromExcel.getDataFromExcelOfNearBy(inputStream,fileName,NearByStore);
    }


    public List<TestInfoBean> AsyncMonitorResult(String ZsetKey,String LocalName){
        Jedis Jedis = TestRedisPool.getJedisPoolInstance(RedisConfig.redisIp, Integer.parseInt(RedisConfig.redisPort),RedisConfig.redisPassword).getResource();
        Long zcount = Jedis.zcount(ZsetKey, 0.0, 1.1);
        if (zcount - 1 <= 0 ) {
        return null;
        }

        Map<byte[], byte[]> map = Jedis.hgetAll(LocalName.getBytes());
        ArrayList<byte[]> bytes = new ArrayList<>(map.values());
        Set<String> keys = Jedis.zrangeByScore(ZsetKey, 0.6,1.1 );
        List<TestInfoBean> InfoBean = new ArrayList<>();
        for (byte[] aByte : bytes) {
            Map<String, String> map1 = SerializeUtil.mapStringToMap(new String(aByte, StandardCharsets.UTF_8));
            if (keys.contains(map1.get("UUid"))){
                // TODO: 2020/9/22   map --> javabean
                TestInfoBean testInfoBean = JSON.parseObject(JSON.toJSONString(map1), TestInfoBean.class);
                testInfoBean.setRate(Jedis.zscore(ZsetKey,map1.get("UUid")));
                if (Double.parseDouble(testInfoBean.getPriceByMonth()) == 0.0){
                    testInfoBean.setPriceByMonth("面谈");
                }
                InfoBean.add(testInfoBean);
            }
        }
        Collections.sort(InfoBean, new Comparator<TestInfoBean>() {
            @Override
            public int compare(TestInfoBean o1, TestInfoBean o2) {
               return o2.getRate().compareTo(o1.getRate());
            }
        });
        return InfoBean;
    }

    public List<storeInfo> getRedisDataForNearBy(String ZsetKey,String LocalName) {
        Jedis Jedis = TestRedisPool.getJedisPoolInstance(RedisConfig.redisIp, Integer.parseInt(RedisConfig.redisPort),RedisConfig.redisPassword).getResource();
        Map<byte[], byte[]> map = Jedis.hgetAll(LocalName.getBytes());
        if (map == null) {
            return null;
        }
        ArrayList<byte[]> bytes = new ArrayList<>(map.values());
        List<storeInfo> InfoBean = new ArrayList<>();
        for (byte[] aByte : bytes) {
                Map<String, String> map1 = SerializeUtil.mapStringToMap(new String(aByte, StandardCharsets.UTF_8));
                // TODO: 2020/9/22   map --> javabean
                storeInfo storeInfo = JSON.parseObject(JSON.toJSONString(map1), storeInfo.class);
                InfoBean.add(storeInfo);
            }
        return InfoBean;
    }

}


